package my.diplom.web.validators;

import org.springframework.validation.Validator;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import my.diplom.entity.Category;
import my.diplom.entity.Event;

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: gb
 * Date: 13.10.2009
 * Time: 0:57:43
 * To change this template use File | Settings | File Templates.
 */
public class CreateEventValidator implements Validator {

    @Override
    public boolean supports(Class aClass) {
        return Event.class.equals(aClass);
    }

    @Override
    public void validate(Object o, Errors errors) {
        Event event = (Event) o;
        if (event == null) errors.reject("Null event received");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "eventName", "eventName.empty", "event Name is required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "startDate", "startDate.empty", "startDate is required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "finishDate", "finishDate.empty", "finishDate is required");
       // ValidationUtils.rejectIfEmptyOrWhitespace(errors, "project", "project.empty", "project is required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "resource", "resource.empty", "resource is required");
        Date start = event.getStartDate();
        Date finish = event.getFinishDate();
        if (start != null && finish != null && start.after(finish)) {
            errors.rejectValue("startDate", "invalid.startDate", "startDate must be less than finishDate");
            errors.rejectValue("finishDate", "invalid.finishDate", "finishDate must be more than startDate!");

            //ValidationUtils.rejectIfEmptyOrWhitespace(errors, "startDate", "startDate.invalid", "startDate must be less than finishDate");
            //ValidationUtils.rejectIfEmptyOrWhitespace(errors, "finishDate", "finishDate.invalid", "finishDate must be more than startDate");
        }

    }
}
